package com.sanpu.kxe.module.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.business.card.bean.BankInfoOutput;
import com.sanpu.kxe.business.trade.interfaces.IComfirmClickListener;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.jump.imageloader.JKImageLoader;
import com.sanpu.kxe.module.roundview.RoundTextView;

import java.math.BigDecimal;

import butterknife.ButterKnife;

/**
 * Description：收款确认页
 * Created by 薛勇军 on 2019/9/25.
 */

public class GatherComfirmDialog {

    private ImageView closeIv, outBankIv, stlBankIv;

    private RoundTextView comfirmRtv;

    private TextView amountTv, outBankTv, stlBankTv, stlBankAmountTv;

    private IComfirmClickListener mClickListener;

    private Dialog aDialog;

    private BankInfoOutput mOutput, mStlBank;

    private String mAmount;

    private double txFee;

    public void setListener(IComfirmClickListener clickListener) {
        this.mClickListener = clickListener;
    }

    public GatherComfirmDialog(
            Context context, BankInfoOutput output, BankInfoOutput stlBank, String amount, double txFee) {
        this.mOutput = output;
        this.mStlBank = stlBank;
        this.mAmount = amount;
        this.txFee = txFee;
        GatherComfirmDialog(context);
    }

    public void GatherComfirmDialog(Context context) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.dialog_gather_comfirm_view, null);
        aDialog = new Dialog(context, R.style.bottom_pop_menu);
        closeIv = ButterKnife.findById(inflate, R.id.closeIv);
        stlBankIv = ButterKnife.findById(inflate, R.id.stlBankIv);
        outBankIv = ButterKnife.findById(inflate, R.id.outBankIv);
        comfirmRtv = ButterKnife.findById(inflate, R.id.comfirmRtv);
        amountTv = ButterKnife.findById(inflate, R.id.amountTv);
        stlBankTv = ButterKnife.findById(inflate, R.id.stlBankTv);
        outBankTv = ButterKnife.findById(inflate, R.id.outBankTv);
        stlBankAmountTv = ButterKnife.findById(inflate, R.id.stlBankAmountTv);
        View emptyView = ButterKnife.findById(inflate, R.id.emptyView);
        emptyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                aDialog.dismiss();
            }
        });

        setUiBeforShow(context);

        aDialog.setContentView(inflate); // 把自定義view加上去
        aDialog.setCanceledOnTouchOutside(false);
        aDialog.setCancelable(false);
        aDialog.show();
    }

    public void setUiBeforShow(Context context) {
        closeIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        comfirmRtv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (mClickListener != null) {
                    mClickListener.onComfirmClick();
                }
            }
        });
        if (mOutput != null) {
            try {
                JKImageLoader.getInstance().displayImage(context,
                        UrlConstants.getBaseUrl() + mOutput.getLogoPath(), outBankIv);
                String bankNo = mOutput.getBankNo();
                outBankTv.setText(mOutput.getBankName().replace(" ", "") + "  (" +
                        bankNo.substring(bankNo.length() - 4, bankNo.length()) + ")");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (mStlBank != null) {
            try {
                JKImageLoader.getInstance().displayImage(context,
                        UrlConstants.getBaseUrl() + mStlBank.getLogoPath(), stlBankIv);
                String bankNo = mStlBank.getBankNo();
                stlBankTv.setText(mStlBank.getBankName().replace(" ", "") + "  (" +
                        bankNo.substring(bankNo.length() - 4, bankNo.length()) + ")");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (!TextUtils.isEmpty(mAmount)) {
            amountTv.setText(mAmount);
            String stlBankAmount = new BigDecimal(mAmount).subtract(new BigDecimal(txFee)).toPlainString();
            if (!TextUtils.isEmpty(stlBankAmount)) {
                stlBankAmountTv.setText(stlBankAmount);
            }
        }
    }

    public void dismiss() {
        if (aDialog != null && aDialog.isShowing()) {
            aDialog.dismiss();
        }
    }

    public void show() {
        if (aDialog != null && !aDialog.isShowing()) {
            aDialog.show();
        }
    }
}
